﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.News.ContentCategory;
using BCService.Common.News.ContentCategory;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Management.Common.News.ContentCategories
{
    /// <summary>
    /// 内容分类控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class ContentCategoriesController : ManagementBaseController
    {
        private readonly IContentCategoryService contentCategoryService;
        /// <summary>
        /// 内容分类控制器
        /// </summary>
        public ContentCategoriesController(IContentCategoryService contentCategoryService)
        {
            this.contentCategoryService = contentCategoryService;
        }
        /// <summary>
        /// 获取内容分类列表（分页）
        /// </summary>
        /// <param name="categoryLevel">分类等级</param>
        /// <param name="categoryName">分类名称</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="ascending">排序</param>
        /// <param name="index">当前页码</param>
        /// <param name="size">每页数据条数</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<ContentCategoryDto>), 200)]
        public IActionResult Get(
            [FromQuery(Name = "category_level")] int? categoryLevel,
            [FromQuery(Name = "category_name")]string categoryName,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "index")]int index = 1, 
            [FromQuery(Name = "size")]int size = 10)
        {
            return this.Ok(this.contentCategoryService.Query(categoryLevel, categoryName, start, end, sortName, ascending, index, size));
        }

        /// <summary>
        /// 获取某一内容分类信息
        /// </summary>
        /// <param name="contentCategoryId">内容分类ID</param>
        /// <returns></returns>
        [HttpGet("{content_category_id}")]
        [ProducesResponseType(typeof(ContentCategoryDto), 200)]
        public IActionResult Get([FromRoute(Name = "content_category_id")]int contentCategoryId)
        {
            return this.Ok(this.contentCategoryService.GetContentCategoryById(contentCategoryId));
        }
        /// <summary>
        /// 获取一级分类列表
        /// </summary>
        /// <param name="level">分类级别</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<ContentCategoryDto>), 200)]
        public IActionResult LevelCategory([FromQuery(Name = "level")]int level = 1)
        {
            return this.Ok(this.contentCategoryService.GetLevelCategoryList(level));
        }

        /// <summary>
        /// 根据分类ID获取下级分类
        /// </summary>
        /// <param name="contentCategoryId">内容分类Id</param>
        /// <returns></returns>
        [HttpGet("NextCategory/{content_category_id}")]
        [ProducesResponseType(typeof(ContentCategoryDto), 200)]
        public IActionResult GetNextCategoryByID([FromRoute(Name = "content_category_id")]int contentCategoryId)
        {
            return this.Ok(this.contentCategoryService.GetNextCategoryById(contentCategoryId));
        }
        /// <summary>
        /// 创建内容分类
        /// </summary>
        /// <returns></returns>
        /// <response code="201">创建一个新的内容分类。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        [HttpPost]
        [Log("添加内容分类")]
        [ProducesResponseType(typeof(ContentCategoryDto), 201)]
        public IActionResult Post([FromBody]ContentCategoryRequestDto dto)
        {
            var result = this.contentCategoryService.Create(dto);

            return this.Created(string.Format("/api/v1/sim/ContentCategory/{0}", result.ContentCategoryId), result);
        }

        /// <summary>
        /// 修改内容分类
        /// </summary>
        /// <returns></returns>
        /// <response code="200">修改后的内容分类信息。</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        [HttpPut("{content_category_id}")]
        [Log("修改内容分类")]
        [ProducesResponseType(typeof(ContentCategoryDto), 200)]
        public IActionResult Update([FromRoute(Name = "content_category_id")]int contentCategoryId, [FromBody]ContentCategoryRequestDto dto)
        {
            return this.Ok(this.contentCategoryService.Update(contentCategoryId, dto));
        }

        /// <summary>
        /// 删除内容分类
        /// </summary>
        /// <returns></returns>
        /// <response code="204">刪除內容分类成功。</response>
        [HttpDelete("{content_category_id}")]
        [Log("删除内容分类")]
        public IActionResult Delete([FromRoute(Name = "content_category_id")]int contentCategoryId)
        {
            this.contentCategoryService.Delete(contentCategoryId);
            return NoContent();
        }

        #region 启用/禁用内容分类
        /// <summary>
        ///  启用/禁用内容分类
        /// </summary>
        /// <param name="contentCategoryId">内容分类Id</param>
        /// <param name="enabled">是否启用</param>
        /// <response code="204">启用/禁用内容分类成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPut("[action]/{content_category_id}/{enabled}")]
        [Log("启用/禁用内容分类")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult Enabled([FromRoute(Name = "content_category_id")]int contentCategoryId,
            [FromRoute(Name = "enabled")]bool enabled)
        {
            if (enabled)
            {
                this.contentCategoryService.Enabled(contentCategoryId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
            else
            {
                this.contentCategoryService.Disabled(contentCategoryId);
                return StatusCode((int)HttpStatusCode.NoContent);
            }
        }
        #endregion
    }
}
